/*
 * Copyright (c) Eldanichu  DMR.js (2024-9-12).
 */
import { RequestAPI } from '@/utils/RequestAPI'
import { StoreUtils } from '../utils/StoreUtils'
import { defineStore } from 'pinia'
import { StringUtils } from '../utils/StringUtils'
import { ParamUtil } from '../utils/convertable/ParamUtil'

export const useDialogStore = defineStore(
  'DialogStoreService',
  {
    state() {
      return {}
    },
    actions: {
      // 领料单
      async getMIList( payload = {}) {
        let api = new RequestAPI()
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/storesgoods"),
            method: "get",

            params: {
              ...payload
            }
          })

          const { data, msg, code } = res

          const { Rows = [], Total = 0 } = data || {}

          return {
            Rows,
            Total,
            msg,
            code
          }
        } catch (e) {
          throw new Error(e)
        }
      },
      async getMIDetail( payload) {
        let api = new RequestAPI()
        const { id } = payload || {}

        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("storesgoods", id),
            method: "get",
          })
          const { data } = res
          return data
        } catch (e) {
          throw new Error(e)
        }
      },

      // 销售单
      async getSaleList( payload = {}) {
        let api = new RequestAPI()
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/saleorder"),
            method: "get",
            params: {
              ...payload
            }
          })

          const { data, msg, code } = res
          const { Rows = [], Total = 0 } = data || {}
          return {
            Rows,
            Total,
            msg,
            code
          }
        } catch (e) {
          throw new Error(e)
        }
      },
      async getSaleDetail( payload = {}) {
        let api = new RequestAPI()
        const { id } = payload
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/saleorder", id),
            method: "get",
          })

          const { data, msg, code } = res
          return {
            data,
            msg,
            code
          }
        } catch (e) {
          throw new Error(e)
        }
      },

      // 销售退单
      async getSaleRefundList( payload = {}) {
        let api = new RequestAPI()
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/salereturnorder"),
            method: "get",
            params: {
              ...payload
            }
          })

          const { data, msg, code } = res
          const { Rows = [], Total = 0 } = data || {}
          return {
            Rows,
            Total,
            msg,
            code
          }
        } catch (e) {
          throw new Error(e)
        }
      },
      async getSaleRefundDetail( payload = {}) {
        let api = new RequestAPI()
        const { id } = payload
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/salereturnorder", id),
            method: "get",
          })

          const { data, msg, code } = res
          return {
            data,
            msg,
            code
          }
        } catch (e) {
          throw new Error(e)
        }
      },

      // 采购单
      async getPOList( payload = {}) {
        let api = new RequestAPI()
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/purchaseorder"),
            method: "get",
            params: {
              ...payload
            }
          })

          const { data, msg, code } = res
          const { Rows = [], Total = 0 } = data || {}
          return {
            Rows,
            Total,
            msg,
            code
          }
        } catch (e) {
          throw new Error(e)
        }
      },
      async getPODetail( payload = {}) {
        let api = new RequestAPI()
        const { id } = payload

        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("purchaseorder", id),
            method: "get",
          })

          const { data, msg, code } = res
          return {
            data,
            msg,
            code
          }
        } catch (e) {
          throw new Error(e)
        }
      },

      // 采购退单
      async getPORefundList( payload = {}) {
        let api = new RequestAPI()
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/purchasereturnorder"),
            method: "get",
            params: {
              ...payload
            }
          })

          const { data, msg, code } = res
          const { Rows = [], Total = 0 } = data || {}
          return {
            Rows,
            Total,
            msg,
            code
          }
        } catch (e) {
          throw new Error(e)
        }
      },
      async getPORefundDetail( payload = {}) {
        let api = new RequestAPI()
        const { id } = payload
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/purchasereturnorder", id),
            method: "get",
          })

          const { data, msg, code } = res
          return {
            data,
            msg,
            code
          }
        } catch (e) {
          throw new Error(e)
        }
      },


      async getBwaDetail( payload) {
        let api = new RequestAPI()
        const { ids = [], biiId ,biiName} = payload || {}
        const ids_str = ids.join(",")
        try {
          const res = await api.request({
            url: StoreUtils.parseUrl("/storage/inv/page"),
            method: "get",
            params: {
              limit: -1,
              size: -1,
              bwaIds: ids_str,
              biiId,
              biiName
            }
          })
          const { data, msg } = res
          return { data, msg }
        } catch (e) {
          throw new Error(e)
        }
      }
    }
  })

